package com.heima.datasync.service;

import com.alibaba.fastjson.JSON;
import com.heima.feigns.article.ArticleFeign;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.search.vo.SearchArticleVo;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j

public class EsDataServiceimpl implements EsDataService {
    @Resource
    ArticleFeign articleFeign;

    @Resource
    RestHighLevelClient restHighLevelClient;

    //导入数据到es
    @Override
    public ResponseResult dataInit() {
        //导入之前先清空索引库
        DeleteRequest request = new DeleteRequest("app_info_article");
        try {
            restHighLevelClient.delete(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            log.error("删除es数据失败====>{}", e);
            e.printStackTrace();
        }


        //先查询出mysql中的文章
        List<ApArticle> articleList = articleFeign.findAllArticles();

        //判断查询的数据是否为空
        if (articleList == null || articleList.size() < 1) {
            log.info("mysql文章内容为空");
            return ResponseResult.okResult();
        }


        ArrayList<SearchArticleVo> searchArticleVos = new ArrayList<>();
        //把文章转换成es需要的vo对象
        for (ApArticle apArticle : articleList) {
            SearchArticleVo searchArticleVo = new SearchArticleVo();
            BeanUtils.copyProperties(apArticle, searchArticleVo);
            searchArticleVos.add(searchArticleVo);
        }

        //创建批处理对象
        BulkRequest bulkRequest = new BulkRequest("app_info_article");

        //导入数据优化批量添加
        int pageNam = searchArticleVos.size() / 5000;
        if (searchArticleVos.size() % 5000 != 0) {
            pageNam += 1;
        }
        //定义初始值
        int startingValue = 0;

        for (int i = 1; i <= pageNam; i++) {
            for (int j = startingValue; j < 5000 * i; j++) {
                IndexRequest indexRequest = new IndexRequest();
                SearchArticleVo searchArticleVo = searchArticleVos.get(startingValue);
                indexRequest.id(searchArticleVo.getId() + "");
                indexRequest.source(JSON.toJSONString(searchArticleVo), XContentType.JSON);
                bulkRequest.add(indexRequest);
                startingValue += 1;
            }
        }

     /*   //创建添加数据对象

        for (SearchArticleVo searchArticleVo : searchArticleVos) {
            IndexRequest indexRequest = new IndexRequest();
            indexRequest.id(searchArticleVo.getId() + "");
            indexRequest.source(JSON.toJSONString(searchArticleVo), XContentType.JSON);
            bulkRequest.add(indexRequest);
        }*/

        //导入
        try {
            restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            log.error("es导入数据失败：===> {}", e);
            e.printStackTrace();
        }
        return ResponseResult.okResult();
    }
}
